//
//  T3FTNPolyline.swift
//  t3_map
//
//  Created by Cunhai Li on 2024/10/22.
//

import QMapKit
import UIKit

class T3FTNPolyline: NSObject, T3FPolyline {

    private(set) var options: T3FPolylineOptions
    /// SDK内置的Polyline
    let realPolyline: T3FTNRealPolyline
    
    weak var polylineView: QPolylineView?

    init(options: T3FPolylineOptions, realPolyline: T3FTNRealPolyline) {
        self.options = options
        self.realPolyline = realPolyline
    }

    func update(options: T3FPolylineOptions) {
        self.options = options
    }
}

/// 用于区分自定义路线和SDK内部添加的路线
class T3FTNRealPolyline: QPolyline { }

extension QTexturePolylineView {
    convenience init(from: T3FTNPolyline) {
        self.init(polyline: from.realPolyline)
        setup(options: from.options)
    }
    
    func setup(options: T3FPolylineOptions) {
        let drawStyleIndexes = options.lineIndexes
        if drawStyleIndexes.count >= 1 {
            var styles: [QSegmentStyle] = []
            var colors: [QSegmentColor] = []
            for index in 0..<drawStyleIndexes.count {
                let start = index == 0 ? 0 : drawStyleIndexes[index-1]
                let end = drawStyleIndexes[index]
                let style = QSegmentStyle()
                style.startIndex = Int32(start)
                style.endIndex = Int32(end)
                style.colorImageIndex = Int32(options.lineImagesIndexs[safe: index] ?? 0)
                styles.append(style)
                
                let segmentColor = QSegmentColor()
                segmentColor.startIndex = Int32(start)
                segmentColor.endIndex = Int32(end)
                let colorType = options.lineImagesIndexs[safe: index] ?? 0
                segmentColor.color = options.lineColors[safe: colorType]
                colors.append(segmentColor)
            }
//            segmentStyle = styles
            segmentColor = colors
        } else {
            let imageIsEmpty = (options.lineImages.count ?? 0) <= 0
            if imageIsEmpty {
                drawType = .repeatDraw
            }
            let style = QSegmentStyle()
            style.startIndex = Int32(0)
            style.endIndex = Int32(options.points.count-1)
            style.colorImageIndex = Int32(0)
            segmentStyle = [style]
        }
//        if options.lineImages.count > 0 {
//            let image = Self.mergedImages(imagesArray:  options.lineImages) ?? UIImage()
//            styleTextureImage = image
//        }
        lineWidth = options.width
        fillColor = options.fillColor
        strokeColor = options.strokeColor
    }
    
    
    static func mergedImages(imagesArray: [UIImage]) -> UIImage? {
        
        guard !imagesArray.isEmpty else {
            return nil
        }
        
        let imageWidth: CGFloat = 10 //考虑手机屏幕宽度
        let totalHeight: CGFloat = CGFloat(imagesArray.count * 1)
        //计算图片的高度
        
        //绘图上下文
        let bound = CGRect(x: 0, y: 0, width: imageWidth, height: totalHeight)
        let format = UIGraphicsImageRendererFormat.default()
        format.scale = 1
        let renderer = UIGraphicsImageRenderer(bounds: bound, format: format)
        let resultingImage = renderer.image { (context) in
            for (index, image) in imagesArray.enumerated() {
                image.draw(in:CGRectMake(0, CGFloat(index), imageWidth, 1))
            }
        }
        
        return resultingImage;
    }
}

extension QPolylineView {
    convenience init(with: T3FTNPolyline) {
        self.init(polyline: with.realPolyline)
        strokeColor = with.options.strokeColor
        fillColor = with.options.fillColor
        lineWidth = with.options.width
        if (with.options.dashType.rawValue > 0) {
            lineDashPattern = [NSNumber(value: 10), NSNumber(value: 10)]
        }
    }
}
